package test.br.com.lh.urabuilder.business.bootstrap
{
	import br.com.lh.urabuilder.business.bootstrap.UraBuilderLoader;
	import br.com.lh.urabuilder.event.LoadUraEvent;
	import br.com.lh.urabuilder.event.LoadUraResourceEvent;
	import br.com.lh.urabuilder.event.dispatcher.MockEventDispatcher;
	import br.com.lh.urabuilder.event.factory.UraEventFactoryMock;
	import br.com.lh.urabuilder.model.UraModelLocator;
	
	import flexunit.framework.EventfulTestCase;
	import flexunit.framework.TestSuite;
	
	import mx.containers.Canvas;

	public class UraBuilderLoaderTest2 extends EventfulTestCase
	{
		public function UraBuilderLoaderTest2(methodName:String=null)
		{
			super(methodName);
		}
		
		public static function suite():TestSuite{
			var ts:TestSuite = new TestSuite();
			ts.addTest(new UraBuilderLoaderTest2("uraNotFoundShouldStoreSessionData"));
			ts.addTest(new UraBuilderLoaderTest2("newUraShouldStoreSessionAndLoadResources"));
			ts.addTest(new UraBuilderLoaderTest2("nullSessionIdShouldNotCallServer"));
			return ts;
		}
		
		public function uraNotFoundShouldStoreSessionData():void{
			var sessionId:String = "sessionIDTeste";
			var uraId:String = "1232";
			var mockedCanvas:Canvas = new Canvas();
			var mockedDispatcher:MockEventDispatcher = new MockEventDispatcher();
			var mockedEventFactory:UraEventFactoryMock = new UraEventFactoryMock();
			var loadUraEvent:LoadUraEvent = new LoadUraEvent(uraId,sessionId);
			mockedEventFactory.expects("getLoadUraEvent").withArgs(LoadUraEvent.EVENT_LOAD_URA_ACTION,sessionId,uraId).willReturn(loadUraEvent);
			mockedDispatcher.expects("dispatchEvent").withArg(loadUraEvent).willReturn(true);
			var loader:UraBuilderLoader = new UraBuilderLoader(mockedDispatcher,mockedEventFactory);
			var modelLocator:UraModelLocator = loader.doLoad(uraId,sessionId,mockedCanvas);
			assertEquals(modelLocator.sessionHolder.uraId,"-1");
			assertEquals(modelLocator.sessionHolder.sessionId,sessionId);
			assertTrue(mockedEventFactory.errorMessage(),mockedEventFactory.success());
			assertTrue(mockedDispatcher.errorMessage(),mockedDispatcher.success());
		}
		
		public function newUraShouldStoreSessionAndLoadResources():void{
			var sessionId:String = "sessionIDTeste";
			var uraId:String = "-1";
			var mockedCanvas:Canvas = new Canvas();
			var mockedDispatcher:MockEventDispatcher = new MockEventDispatcher();
			var mockedEventFactory:UraEventFactoryMock = new UraEventFactoryMock();
			var loadUraEvent:LoadUraResourceEvent= new LoadUraResourceEvent(sessionId);
			mockedEventFactory.expects("getLoadUraEvent").withArgs(LoadUraResourceEvent.EVENT_LOAD_URA_RESOURCE_ACTION,sessionId,uraId).willReturn(loadUraEvent);
			mockedDispatcher.expects("dispatchEvent").withArg(loadUraEvent).willReturn(true);
			var loader:UraBuilderLoader = new UraBuilderLoader(mockedDispatcher,mockedEventFactory);
			var modelLocator:UraModelLocator = loader.doLoad(uraId,sessionId,mockedCanvas);
			assertEquals(modelLocator.sessionHolder.uraId,"-1");
			assertEquals(modelLocator.sessionHolder.sessionId,sessionId);
			assertTrue(mockedEventFactory.errorMessage(),mockedEventFactory.success());
			assertTrue(mockedDispatcher.errorMessage(),mockedDispatcher.success());
		}
		
		public function nullSessionIdShouldNotCallServer():void{
			UraModelLocator.modelLocator = null;
			var sessionId:String = null;
			var uraId:String = "-1";
			var mockedCanvas:Canvas = new Canvas();
			var mockedDispatcher:MockEventDispatcher = new MockEventDispatcher();
			var mockedEventFactory:UraEventFactoryMock = new UraEventFactoryMock();
			var loader:UraBuilderLoader = new UraBuilderLoader(mockedDispatcher,mockedEventFactory);
			var modelLocator:UraModelLocator = loader.doLoad(uraId,sessionId,mockedCanvas);
			assertEquals("-1",modelLocator.sessionHolder.uraId);
			assertEquals(null,modelLocator.sessionHolder.sessionId);
			assertTrue(mockedEventFactory.errorMessage(),mockedEventFactory.success());
			assertTrue(mockedDispatcher.errorMessage(),mockedDispatcher.success());
		}
		
		
		
		
		
	}
}